<?php

class User_Account {
    const USER_V2 = "user_v2";
    const GROUP = "default";

    public static function user_account_info($uid=-1){
        $query = DB::select()->from(self::USER_V2);
        if($uid != -1){
            $query = $query->where('uid','=',$uid);
        }
        $query = $query->execute(self::GROUP);
        return $query;
    }

    public static function create($data){
        list($insert_id, $rows) = DB::insert(self::USER_V2)
            ->columns (array_keys($data))
            ->values (array_values($data))
            ->execute(self::GROUP);
        return $insert_id;
    }

    public static function update($data,$key,$value){
        $rows = DB::update(self::USER_V2)
            ->set($data)
            ->where($key, '=', $value)
            ->execute(self::GROUP);
        return $rows;
    }

    public static function account_checkout($key,$val){
        $query = DB::select()
            ->from(self::USER_V2)
            ->where($key,'=',$val)
            ->execute(self::GROUP);
        return $query;
    }

    public static function guider_account($userName,$type,$limit,$offset,$email=null,$mobile=null){
        $query = DB::select()
            ->from(self::USER_V2)
            ->where('utype','=','GUIDER')
            ->where('status','!=',99)
            ->where('identityName','!=','');
        if(!empty($userName)){
            $query = $query->where('userName','=',$userName);
        }
        if($type == 3){
            $type = 0;
        }
        if(!empty($type) || $type === 0){
            $query = $query->where('identityActive','=',$type);
        }

        if(!empty($email)){
            $query = $query->where('email','=',$email);
        }

        if(!empty($mobile)){
            $query = $query->where('telNum','like','%'.$mobile.'%');
        }
//        echo $query;
        $count = $query->execute()->count();
        $list = $query->order_by('changed','desc')->limit($limit)->offset($offset)
            ->execute()->as_array();
        return array($count,$list);
    }


    public static function reset_pwd($email){

        //保存用户激活token
        $data['token'] = md5($email.time().'wayatrip');
        $data['token_exptime'] = time()+60*30;
        $data['email'] = $email;
        $data['created'] = time();
        list($insert_id, $rows) = DB::insert('reset_pwd')
            ->columns (array_keys($data))
            ->values (array_values($data))
            ->execute(self::GROUP);

        if($rows <= 0){
            return false;
        }
        $info = self::account_checkout('userId',$email)->current();
//        var_dump($info);
//        exit;
        $email_body = "您好，{$info['userName']} ：
请点击下面的链接来重置您的密码。
".url::site("/homepage/passwd/?token=".$data['token'], 'http', FALSE)."
如果您的邮箱不支持链接点击，请将以上链接地址拷贝到你的浏览器地址栏中。
该验证邮件有效期为30分钟，超时请重新发送邮件。
此邮件为系统自动发出的，请勿直接回复。
";
        $status = notice::factory('email')
            ->to($email)
//                ->to($post['email'])
            ->sender('哇吖旅行')
            ->subject("密码操作")
            ->message(nl2br($email_body))
            ->send();
        $data['status'] = $status;

        $match_qq = "#@qq\.com#i";
        $match_163 = "#@163\.com#i";
        $match_waya = "#@wayatrip\.com#i";
        $match_sina = "#@sina\.com.*#i";
        $match_sohu = "#@sohu\.com.*#i";
        $match_126 = "#@126\.com.*#i";
        $match_hotmail = "#@hotmail\.com.*#i";
        $match_outlook = "#@outlook\.com.*#i";
        $match_yahoo = "#@yahoo\.com.*#i";
        $match_gmail = "#@gmail\.com.*#i";
        $match_189 = "#@189\.cn.*#i";
        $match_139 = "#@139\.com.*#i";
        $match_tom = "#@tom\.com.*#i";
//        $rs = ;

        if(preg_match($match_qq,$email)){
            $data['url'] = "https://mail.qq.com";
        }
        elseif(preg_match($match_163,$email)){
            $data['url'] = "http://mail.163.com/";
        }
        elseif(preg_match($match_waya,$email)){
            $data['url'] = "http://qiye.163.com/login/";
        }
        elseif(preg_match($match_sina,$email)){
        	$data['url'] = "http://mail.sina.com.cn/";
        }
        elseif(preg_match($match_sohu,$email)){
        	$data['url'] = "http://mail.sohu.com/";
        }
        elseif(preg_match($match_126,$email)){
        	$data['url'] = "http://mail.126.com/";
        }
        elseif(preg_match($match_hotmail,$email)||preg_match($match_outlook,$email)){
        	$data['url'] = "http://www.hotmail.com/";
        }
        elseif(preg_match($match_yahoo,$email)){
        	$data['url'] = "http://login.yahoo.com/";
        }
        elseif(preg_match($match_gmail,$email)){
        	$data['url'] = "http://www.gmail.com/";
        }
        elseif(preg_match($match_189,$email)){
        	$data['url'] = "http://webmail30.189.cn/";
        }
        elseif(preg_match($match_139,$email)){
        	$data['url'] = "http://mail.10086.cn/";
        }
        elseif(preg_match($match_tom,$email)){
        	$data['url'] = "http://web.mail.tom.com/";
        }
        
        else{
            $data['url'] = "";
        }

        return $data;
    }

    public static function valid_token($token){
        $rs = DB::select()
            ->from('reset_pwd')
            ->where('token','=',$token)
            ->execute()
            ->current();

        $ret = array();
        if(empty($rs)){
            $ret['status'] = false;
            $ret['msg'] = '验证失败';
        }

        if(time() - $rs['token_exptime'] > 30*60){
            $ret['status'] = false;
            $ret['msg'] = '邮件过期，请重新发送邮件';
        }else{
            $ret['status'] = true;
            $ret['token'] = $rs['token'];
            $ret['email'] = $rs['email'];
            $ret['msg'] = '验证成功';
        }
        return $ret;
    }


}
